package com.ict.ms.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ict.ms.targetLocation.facade.model.targetLocation;
import com.ict.ms.common.StateMsg;
import com.ict.ms.targetLocation.facade.api.ITargetLocationService;
import com.ict.ms.targetLocation.facade.common.PageUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenlong on 2016/12/6.
 */

/**
 * 目标快速定位
 */

@Controller
@RequestMapping({"/realtime"})
public class TargetLocationController {
    @Resource
    ITargetLocationService targetLocationService;

    /**
     * 获取目标列表
     * @param map
     * @param num
     * @param type
     * @param mode
     * @param pageid
     * @param pagecount
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "targetlocation.do",method = RequestMethod.POST)
    public String getDegInfo(ModelMap map, @RequestParam(value="num", required = false) String num,
                             @RequestParam(value="type", required = false) int type, @RequestParam(value="mode", required = false) int mode,
                             @RequestParam(value="pageid", required = false) int pageid, @RequestParam(value="pagecount", required = false) int pagecount){
        String msg;
        String jsonStr;

        if(mode==0){
            int count=targetLocationService.targetFuseLocation(num, type);
            if(count==0){
                msg="该目标不存在";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(0, map);
                return jsonStr;
            }

            if(count==-1){
                msg="查询失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-1, map);
                return jsonStr;
            }

            List<targetLocation> fuseLocationList=targetLocationService.pageFuseLocation(num,type,pageid,pagecount);
            if(fuseLocationList.size()==0){
                msg="查询失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-2, map);
                return jsonStr;
            }

            //计算最大页码
            int maxPage= PageUtils.getMaxPage(count,pagecount);

            map.addAttribute("total",count);
            map.addAttribute("max_page",maxPage);
            map.addAttribute("shipList",fuseLocationList);

            jsonStr = StateMsg.toJson(1, map);
            return jsonStr;
        }else if(mode==1){
            int count=targetLocationService.targetOriginLocation(num, type);
            if(count==0){
                msg="该目标不存在";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-1, map);
                return jsonStr;
            }

            if(count==-1){
                msg="查询失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(0, map);
                return jsonStr;
            }

            List<targetLocation> originLocationList=targetLocationService.pageOriginLocation(num,type,pageid,pagecount);
            if(originLocationList.size()==0){
                msg="查询失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-2, map);
                return jsonStr;
            }

            //计算最大页码
            int maxPage= PageUtils.getMaxPage(count,pagecount);

            map.addAttribute("total",count);
            map.addAttribute("max_page",maxPage);
            map.addAttribute("shipList",originLocationList);

            jsonStr = StateMsg.toJson(1, map);
            return jsonStr;
        }else{
            msg="参数传递错误";
            map.addAttribute("error",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }
    }
}